// pages/process/index/index.js
const app = getApp()
import { toast } from '../../../../utils/Tools.js'
import { handletabChange } from '../../../utils/Tools'
import {getDriverOrder,sendTakeGoods} from '../../../network/process'
Page({
  data: {
    currentTabIndex:0,
    titles:['未进行', '进行中', '已完成'],
    list: [],
    unConduct:[],
    conducting:[],
    finish: [],
    isIPhone: false
  },

  onLoad() {
    wx.hideHomeButton()
    this.setData({list:app.globalData.list,isIPhone: app.globalData.isIPhone})
  },
  onShow(){
    //          调用网络请求方法         //
    this._getDriverOrder()
  },

  //    事件函数      //
  tabChange(e) {
    const {index} = e.detail
    handletabChange(index)
  },
  handleGps(e){ //   处理点击导航事件
    wx.navigateTo({
      url:'/pages/other/map/map?address=' + e.currentTarget.dataset.address
    })
  },
  handleCheckDetail(e){  //   处理点击查看详情事件
    wx.navigateTo({
      url: "/driver_side/pages/order-hall/orderDetail/orderDetail?id=" + e.currentTarget.dataset.id 
    })
  },
  handlePhone(e){   //   处理拨打电话
    const {phone} = e.currentTarget.dataset
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: ()=>{
        return 0;
      }
    });
  },
  onTabsItemTap:function(e){
    let {index} = e.currentTarget.dataset;
    this.setData({
      currentTabIndex:index
    })
  },
  handleGoods(e){
    const {msg,id} = e.currentTarget.dataset;
    wx.showModal({
      title: `确认${msg}`,
      content: `您是否确认${msg}`,
      success: (result) => {
        if (result.confirm) {
          switch(msg){
            case '已提货':
              sendTakeGoods(id).then(res=>{
                if(res.code=== 200){
                  toast('提货成功','success')
                  this._getDriverOrder()
                  this.setData({
                    currentTabIndex:1
                  })
                }
              })
              break;
            case '已完成':
              wx.navigateTo({
                url: '/driver_side/pages/process/process-finish/process-finish?id=' + id
              });
              break;
          }
        }else{
          toast(`您取消了${msg}`)
        }
      }
    });
      
  },
  //     网络请求      //
  _getDriverOrder(){
    getDriverOrder().then(res=>{
      if(res.code === 200){
        const unConduct = res.data.filter(res=>{
          return res.driver_status.toString() === "0"
        })
        const conducting = res.data.filter(res=>{
          return res.driver_status.toString() === "1"
        })
        const finish = res.data.filter(res=>{
          return res.driver_status.toString() === "2"
        })
        this.setData({
          unConduct,conducting,finish
        })
        if(conducting.length>0){
          clearInterval(app.globalData.driverTask)
        }else{
          app.globalData.driverTask = setInterval(app._task,60000)
        }
      }
    })
  }
})
